package cn.iocoder.yudao.module.scenic.controller.admin.spotrelation;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.scenic.controller.admin.spotrelation.vo.SpotRelationPageReqVO;
import cn.iocoder.yudao.module.scenic.controller.admin.spotrelation.vo.SpotRelationRespVO;
import cn.iocoder.yudao.module.scenic.controller.admin.spotrelation.vo.SpotRelationSaveReqVO;
import cn.iocoder.yudao.module.scenic.dal.dataobject.spotrelation.SpotRelationDO;
import cn.iocoder.yudao.module.scenic.service.spotrelation.SpotRelationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 线路实践点关联")
@RestController
@RequestMapping("/scenic/spot-relation")
@Validated
public class SpotRelationController {

    @Resource
    private SpotRelationService spotRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建线路实践点关联")
    @PreAuthorize("@ss.hasPermission('road:spot-relation:create')")
    public CommonResult<Long> createSpotRelation(@Valid @RequestBody SpotRelationSaveReqVO createReqVO) {
        return success(spotRelationService.createSpotRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新线路实践点关联")
    @PreAuthorize("@ss.hasPermission('road:spot-relation:update')")
    public CommonResult<Boolean> updateSpotRelation(@Valid @RequestBody SpotRelationSaveReqVO updateReqVO) {
        spotRelationService.updateSpotRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除线路实践点关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('road:spot-relation:delete')")
    public CommonResult<Boolean> deleteSpotRelation(@RequestParam("id") Long id) {
        spotRelationService.deleteSpotRelation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得线路实践点关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('road:spot-relation:query')")
    public CommonResult<SpotRelationRespVO> getSpotRelation(@RequestParam("id") Long id) {
        SpotRelationDO spotRelation = spotRelationService.getSpotRelation(id);
        return success(BeanUtils.toBean(spotRelation, SpotRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得线路实践点关联分页")
    @PreAuthorize("@ss.hasPermission('road:spot-relation:query')")
    public CommonResult<PageResult<SpotRelationRespVO>> getSpotRelationPage(@Valid SpotRelationPageReqVO pageReqVO) {
        PageResult<SpotRelationDO> pageResult = spotRelationService.getSpotRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpotRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出线路实践点关联 Excel")
    @PreAuthorize("@ss.hasPermission('road:spot-relation:export')")
    @OperateLog(type = EXPORT)
    public void exportSpotRelationExcel(@Valid SpotRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpotRelationDO> list = spotRelationService.getSpotRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "线路实践点关联.xls", "数据", SpotRelationRespVO.class,
                        BeanUtils.toBean(list, SpotRelationRespVO.class));
    }

}